//
//  BsCollectionView.m
//  Demo
//
//  Created by colawh on 2019/3/16.
//  Copyright © 2019年 colawhdfws. All rights reserved.
//

#import "BsCollectionView.h"
#import "BsCollectionViewCell.h"
#import "Header.h"

static NSString *const IDE = @"bs_ide___";
@interface BsCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *collectionView;


@end


@implementation BsCollectionView

-(instancetype)initWithItemArray:(NSArray *)itemsArray{
    if ([super init]) {
        self.dataSource = [itemsArray mutableCopy];
        [self configUI];
    }
    return self;
}
- (CGFloat)realHeight{
    return _collectionView.contentSize.height;
}
- (void)configUI{
    [self addSubview:self.collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSString *titleStr = self.dataSource[indexPath.row];
    BsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IDE forIndexPath:indexPath];
    cell.textLb.text = titleStr;
    cell.textLb.textColor = kUIColorFromRGBA(0x091522, 0.55);
    cell.textLb.backgroundColor = kUIColorFromRGB(0xF8F8F8);
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *titleStr = self.dataSource[indexPath.row];
    NSInteger lenght = titleStr.length>=10?10:titleStr.length;
    return CGSizeMake(lenght*15+4, 18);
}
- (void)layoutSubviews{
    [super layoutSubviews];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10 ;
        flowLayout.sectionInset =UIEdgeInsetsMake(15, 15, 15, 15);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[BsCollectionViewCell class] forCellWithReuseIdentifier:IDE];
        _collectionView.bounces = NO;
    }
    return _collectionView;
}
@end
